const app = getApp()
Page({
    data: {
        content: '',
        header: '',
        realName: '',
        title: '',
        updateTime: '',
        nice: 0,
        views: 0,
        commentsNum: 0,
        imgs: [],
        commentsType: false,
        inputValue: '',
        tokenID: '',
        moment: [],
        page: 1,
        datamsg: false,
        sysid: '',
        scale:false,
        maskShow: false,
        deleteSysid:'',
        deleteIndex:0
    },
    onLoad: function(options) {
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID,
            sysid: options.sysid
        })
        wx.request({
            url: app.data.api + '/questions/questionsinfo',
            data: {
                "sysid": _this.data.sysid,
                "token": _this.data.tokenID
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    var updateTime = res.data.data.updateTime;
                    if (updateTime == null){
                        createTime = null;
                    }else{
                        var sitem = res.data.data.updateTime,
                            createTime = sitem.substr(6, 4);
                    }
                    _this.setData({
                        content: res.data.data.content,
                        header: res.data.data.header,
                        realName: res.data.data.realName,
                        title: res.data.data.title,
                        updateTime: createTime,
                        nice: res.data.data.nice,
                        views: res.data.data.views,
                        commentsNum: res.data.data.commentsNum,
                        imgs: res.data.data.imgs
                    })
                } else {

                }
            }
        })
        _this.onReachBottom();
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        wx.showLoading({
            title: '玩命加载中',
        })
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + '/questions/getquestionsanswerslist',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID,
                "sysid": that.data.sysid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = that.data.moment;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var json = {};
                            var str = res.data.data.list[i].createTime,
                                createTime = str.substr(6, 4);
                            json.header = res.data.data.list[i].header,
                                json.realName = res.data.data.list[i].realName,
                                json.content = res.data.data.list[i].content,
                                json.department = res.data.data.list[i].department,
                                json.delete = res.data.data.list[i].delete,
                                json.sysid = res.data.data.list[i].sysid,
                                json.createTime = createTime,
                                moment_list.push(json);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: that.data.moment
                        })
                        if (that.data.page > 1)
                        {
                            if (that.data.moment.length  > 0) {
                                if (res.data.data.list.length == 0) {
                                    that.setData({
                                        datamsg: true
                                    })
                                }
                            }
                        }
                      
                    } else {
                        var moment_list = [];

                        for (var i = 0; i < res.data.data.list.length; i++) {
                            var json = {};
                            var str = res.data.data.list[i].createTime,
                                createTime = str.substr(6, 4);
                            json.header = res.data.data.list[i].header,
                                json.realName = res.data.data.list[i].realName,
                                json.content = res.data.data.list[i].content,
                                json.department = res.data.data.list[i].department,
                                json.delete = res.data.data.list[i].delete,
                                json.sysid = res.data.data.list[i].sysid,
                                json.createTime = createTime,
                                moment_list.push(json);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    showCommentsInput: function() {
        var _this = this;
        _this.setData({
            commentsType: true
        })
    },
    commentsInputBlur: function() {
        var _this = this;
        _this.setData({
            commentsType: false
        })
    },
    commentsInput: function(event) {
        var _this = this,
            inputVal = event.detail.value;
        _this.setData({
            inputValue: inputVal
        })
    },
    imgshow: function(event) {
        var _this = this;
        var src = event.currentTarget.dataset.src;
        var imgList = [];
        var imgs = _this.data.imgs;
        imgList.push(src)
        wx.previewImage({
            current: src, 
            urls: imgs 
        })
    },
    submitBtn: function() {
        var _this = this;
        if (_this.data.inputValue.length == 0) {
            wx.showToast({
                title: '请输入评论',
                image: '../../images/warning.png',
                duration: 800,
            })
            return false
        }
        wx.showToast({
            title: '提交评论中...',
            icon: 'loading',
            mask: true,
            duration: 10000
        })
        wx.request({
            url: app.data.api + '/questions/savequestionsanswers',
            data: {
                "token": _this.data.tokenID,
                "sysid": _this.data.sysid,
                "text": _this.data.inputValue
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                var commentsNum = _this.data.commentsNum+1;
                if (res.data.status) {
                    wx.hideToast();
                    _this.setData({
                        inputValue: '',
                        commentsNum: commentsNum
                    })
                    _this.onPullDownRefresh();
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        mask: true,
                        duration: 1000
                    })
                }
            }
        })
    },
    nice:function(){
        var _this = this;
        wx.request({
            url: app.data.api + '/questions/savequestionsnice',
            data: {
                "token": _this.data.tokenID,
                "sysid": _this.data.sysid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                var nice = _this.data.nice +1;
                if (res.data.status) {
                    _this.setData({
                        nice: nice,
                        scale:true
                    })
                    setTimeout(function () {
                        _this.setData({
                            scale: false
                        })
                    }, 1000);
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        mask: true,
                        duration: 1000
                    })
                }
            }
        })
    },
    deleteTopic:function(event){
        var _this = this;
        var sysid = event.currentTarget.dataset.sysid;
        var index = event.currentTarget.dataset.index;
        _this.setData({
            deleteSysid: sysid,
            deleteIndex: index,
            maskShow:true
        })
    },
    cancel:function(){
        this.setData({
            maskShow: false
        })
    },
    ConfirmRemove:function(){
        var _this = this;
        var imgList = _this.data.moment;
        wx.showToast({
            title: '删除中...',
            icon: 'loading',
            mask: true,
            duration: 10000
        })
        wx.request({
            url: app.data.api + '/questions/deletequestionsanswers',
            data: {
                "token": _this.data.tokenID,
                "sysid": _this.data.deleteSysid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.status) {
                    wx.hideToast();
                    _this.onPullDownRefresh();
                    _this.cancel();
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        image: '../../images/warning.png',
                        duration: 800,
                    })
                }
            }
        })
    }
})